<%@ page language="java" contentType="text/javascript; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import='java.sql.*' %>
<%@page import="java.util.ArrayList"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONSerializer"%>
<%@page import="net.sf.json.JSONArray"%>

<%String callback = request.getParameter("callback");%>
<%=callback %> (<%

	String num = request.getParameter("num");
	
	Connection conn = null;
	PreparedStatement pstmt = null;
	
	try{
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection(
			"jdbc:mysql://localhost/itscreater", "itscreater", "campus123");
	
		String select = "select * from PostComment where PostNumber = ?;";
		pstmt = conn.prepareStatement(select);
		pstmt.setString(1, num);
		ResultSet rs = pstmt.executeQuery();
		
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		while (rs.next()) {
			String key = rs.getString("Number");
			String postnum = rs.getString("PostNumber");
			String writerid = rs.getString("WriterID");
			String writer = rs.getString("Writer");
			String text = rs.getString("Text");
			if (text != null) {
				text = text.replace("\n","<br>");
				text = text.replace("\u0020","&nbsp;");
			}
			String date = rs.getString("Date");
			String time = rs.getString("Time");
			
			jo.put("Number", key);
			jo.put("PostNumber", postnum);
			jo.put("WriterID", writerid);
			jo.put("Writer", writer);
			jo.put("Text", text);
			jo.put("Date", date);
			jo.put("Time", time);
			
			ja.add(jo);
		} 		
	
		out.print(ja);	
	}
	
	finally{
		try{
			pstmt.close();
		}
		catch (Exception ignored){
		}
		try{
			conn.close();
		}
		catch (Exception ignored){
		}
	}
%>);